/**
 * @file Service_Operation.cpp
 * @author Glorill 1615688664@qq.com
 * @brief DR16遥控器发包等操作
 * @version 0.1
 * @date 2022-02-04
 * 
 * @copyright Copyright (c) 2022
 * 
 */

/* Includes ------------------------------------------------------------------*/

#include "Service_Operation.h"
#include "System_Config.h"
#include "Service_Communication.h"

#include "Chassis_Operation.h"
#include "Pan_Tilt_Operation.h"
#include "Shoot_Operation.h"

/* Private macros ------------------------------------------------------------*/

#define USART1_RX_BUFFER_SIZE 64

/* Private types -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

TaskHandle_t Operation_Handle;
DR16_Classdef DR16;

//控制模式
E_CtrlMode CtrlMode;
//小陀螺模式使能
bool GyroscopeMode;

uint8_t Uart1_Rx_Buff[USART1_RX_BUFFER_SIZE];

/* Private function declarations ---------------------------------------------*/

uint32_t DR16_recv_Callback(uint8_t *Recv_Data, uint16_t ReceiveLen);
void Task_Operation(void *arg);

/* Function prototypes -------------------------------------------------------*/

/**
 * @brief DR16接收数据回调函数
 * 
 * @param Recv_Data 
 * @param ReceiveLen 
 * @return uint32_t 
 */
uint32_t DR16_recv_Callback(uint8_t *Recv_Data, uint16_t ReceiveLen)
{
    DR16.DataCapture((DR16_DataPack_Typedef *)Recv_Data);
    return 0;
}

/**
 * @brief 下发操作指令的线程
 * 
 * @param arg 
 */
void Task_Operation(void *arg)
{
    for (;;)
    {
        static uint16_t prescale = 0;
        if (prescale++ >= 10)
        {
            prescale = 0;

        }

        DR16.Check_Link(xTaskGetTickCount());
        LinkageStatus_Typedef DR16_status = DR16.GetStatus();
        
        // 遥控器连接
        if (DR16_status == Connection_Established) 
        {
            // 左侧开关决定控制模式
            if (DR16.GetSW_L() == SW_MID)
            {
                CtrlMode = REMOTE_CONTROL;
            }
            else if (DR16.GetSW_L() == SW_UP)
            {
                CtrlMode = KEYBOARD_CONTROL;
            }
            else
            {
                CtrlMode = DISABLE_CONTROL;
            }

            // 键盘控制模式下，开启小陀螺模式
            if (CtrlMode == KEYBOARD_CONTROL)
            {
                GyroscopeMode = true;
            }
            else
            {
                GyroscopeMode = false;
            }
        }
        // 遥控器离线
        else    
        {
            CtrlMode = DISABLE_CONTROL;
        }

        Chassis_Operation();
        Pan_Tilt_Operation();
        Shoot_Operation();

        vTaskDelay(1); // Pass to next ready task
    }
}

/**
 * @brief DR16初始化
 * 
 */
void DR16_Uart_Init(void)
{
    Uart_Init(&huart1, Uart1_Rx_Buff, USART1_RX_BUFFER_SIZE, DR16_recv_Callback);
}

/**
 * @brief 新建线程
 * 
 */
void Service_Operation_Init(void)
{
    xTaskCreate(Task_Operation, "Operation_Service", Normal_Stack_Size, NULL, PriorityBelowNormal, &Operation_Handle);
}

/************************ COPYRIGHT(C) USTC-ROBOWALKER **************************/
